package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarCarStop;
import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.config.ConfigMap;

/**
 * 车辆停车算法.<br>
 * 输出：统计<br/>
 */
@Arithmetic(name = "车辆停车算法")
public class CarStop extends AbstractArithmetic<VarCarStop> {
    /**
     * 停车的转速阀值
     */
    private int CarStop_Rotation_Threshold = ConfigMap.getInteger("CarStop_Rotation_Threshold");

    /**
     * 是否停车<br>
     * 上一个点转速>500并且车速大于0，本次车速 = 0，记录一次
     */
    private boolean isCarStop(){
        CommonData commonData = getCommonData();
        return commonData.getSpeedKmh()==0.0 && commonData.getRotation()>CarStop_Rotation_Threshold;
    }

    /**
     * 是否停车
     */
    private boolean isCarRunning(){
        CommonData commonData = getCommonData();
        return commonData.getSpeedKmh()>0.0 && commonData.getRotation()>CarStop_Rotation_Threshold;
    }

    @Override
    public void calculate() {
        logger.debug("进入停车算法.");
        VarCarStop var = super.getObj();

        //上一次车运行中，本次停车了，则认为停车一次
        if (var.isPreCarRunning() && isCarStop()) {
            var.setNumber(var.getNumber() + 1);
            var.setPreCarRunning(false);
        }else if (isCarRunning()) {
            var.setPreCarRunning(true);
        }

        logger.debug("停车算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        statisticDataOut.setVehicleParkNumber(getObj().getNumber());
    }
}
